<?php

namespace Ad\Logic\Qianka;

use \common\Table\InputClick\AdClickQiankaTable;
use \common\Table\InputClick\AdClickTable;
use \core\filter\filter;
use \common\Factory\Factory;
use \core\data\partitionTable;

class QiankaInputClick {
    
    public function insert($data) {
        Factory::getServer('MqWriteLog')->send('qianka click raw input params:' . json_encode($data));
        //验证及过滤
        $adQiankaClickColumns = array(
            'appid' => FILTER_SANITIZE_STRING,
            'idfa' => FILTER_SANITIZE_STRING, 
            'ip' => array('filter' => FILTER_VALIDATE_IP),
        );
        $adClickColumns = array('idfa' => FILTER_SANITIZE_STRING, 'appid' => FILTER_SANITIZE_STRING);
        $filter = new filter();
        $adQiankaClickColumns = $filter->varArray($data, $adQiankaClickColumns);
        $adClickColumns = $filter->varArray($data, $adClickColumns);
        
        $result = false;
        if($adQiankaClickColumns !== false && $adClickColumns !== false) {
            //合并额外参数
            $timestamp = time();
            $config = \core\init\init::getConfig();
            $adClickColumns['app_id'] = $adClickColumns['appid'];
            unset($adClickColumns['appid']);
            $adQiankaClickColumns = array_merge($adQiankaClickColumns, array('input_timestamp' => $timestamp));
            $adClickColumns = array_merge($adClickColumns, array('type' => $config['adType']['qianka'], 'time' => date('Ym', $timestamp), 'input_timestamp' => $timestamp));
            
            //入库
            $adClickTable = new AdClickTable();
            $adClickQiankaTable = new AdClickQiankaTable();
            $db = $adClickQiankaTable->getDb();
            
            $partitionTable = new partitionTable();
            $db->beginTransaction();
            $tableNum = $partitionTable->getHashTable($adQiankaClickColumns['idfa']);
            $qiankaResult = $adClickQiankaTable->insertAdQiankaClick($adQiankaClickColumns, $tableNum);
            if($qiankaResult === true) {
                $result = $adClickTable->insertAdClick($adClickColumns);
                if($result === true) {
                    $db->commit();
                    Factory::getServer('MqWriteLog')->send('qianka click qianka_click table insert success');
                }
                else {
                    $db->rollBack();
                    Factory::getServer('MqWriteLog')->send('qianka click ad_click table insert failed');
                }
            }
            else {
                $db->rollBack();
                Factory::getServer('MqWriteLog')->send('qianka click qianka_click table insert failed');
            }                
        }
        else {
            Factory::getServer('MqWriteLog')->send('qianka click raw input params validate failed');
        }
        
        if($result === false) {
            header('HTTP/1.1 400');
            header('status: 400');
            Factory::getServer('MqWriteLog')->send('qianka click failed');
        }
        
        return $result;
    }
}